/*
 * EditorTab.java
 *
 * Created on 29. Januar 2008, 19:42
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package de.blitzcoder.collide.config;

import de.blitzcoder.collide.Config;
import de.blitzcoder.collide.gui.textarea.CollIDETextArea;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import de.blitzcoder.collide.util.Log;


/**
 *
 * @author blitzcoder
 */
public class EditorTab extends ConfigTab {
    
    // 2 Panels
    JPanel          editorPanel;
    JPanel          outputPanel;
    
    // tabWidth
    JLabel          tabWidthLabel;
    JSpinner        tabWidthSpinner;

    // softTabs
    JLabel          softTabsLabel;
    JCheckBox       softTabsCheckBox;
    
    // undoLimit
    JLabel          undoLimitLabel;
    JSpinner        undoLimitSpinner;
    
    // Output Font
    JComboBox       outputFontComboBox;
    JSpinner        outputFontSizeSpinner;
    
    
    public String getTitle() {
        return "Editor";
    }
    
    public void createInterface() {
        
        Log.log( "Creating Interface" );
        
        setLayout( new GridBagLayout() );
        
        GridBagConstraints c = new GridBagConstraints();
        
                
        // Create the 2 Panels
        editorPanel = new JPanel();
        editorPanel.setLayout( new GridBagLayout() );
        editorPanel.setBorder(BorderFactory.createTitledBorder("Editor"));
        outputPanel = new JPanel();
        outputPanel.setLayout( new GridBagLayout() );
        outputPanel.setBorder(BorderFactory.createTitledBorder("Output-Fenster"));
        
                
        // Create the Gadgets
        // Editor
            tabWidthLabel = new JLabel("Tab-Breite: ");
            tabWidthSpinner = new JSpinner();
            SpinnerModel tabWidthSpinnerModel = new SpinnerNumberModel( CollIDETextArea.getPropertyManager().getIntProperty("buffer.tabSize"),1,20,1); // Standard Value,min,max,step
            tabWidthSpinner.setModel( tabWidthSpinnerModel );
            // --
            undoLimitLabel = new JLabel( "Rückschritte:" );
            undoLimitSpinner = new JSpinner();
            SpinnerModel undoLimitSpinnerModel = new SpinnerNumberModel( CollIDETextArea.getPropertyManager().getIntProperty("buffer.undoCount"),1,10000,1); // Standard Value,min,max,step
            undoLimitSpinner.setModel( undoLimitSpinnerModel );
            outputFontComboBox = new JComboBox();
            outputFontSizeSpinner = new JSpinner();
            SpinnerModel outputFontSizeSpinnerModel = new SpinnerNumberModel( Config.getIntProperty("output.font.size"),1,120,1); // Standard Value,min,max,step
            outputFontSizeSpinner.setModel( outputFontSizeSpinnerModel );
            softTabsLabel = new JLabel("Soft Tabs (n Leerzeichen statt Tabs)");
            softTabsCheckBox = new JCheckBox();
            softTabsCheckBox.setSelected(CollIDETextArea.getPropertyManager().getBooleanProperty("buffer.noTabs"));


        // Add the gadgets
        // Editor
            c.gridx = 0;
            c.insets = new Insets(15,15,15,15);
            c.gridy = 0;
            c.weightx = 0.0;
            //c.fill = c.BOTH;
            c.weighty = 0.0;
            c.anchor = c.LINE_START;
            editorPanel.add( tabWidthLabel , c );
            c.gridy = 1;
            editorPanel.add( undoLimitLabel , c);
            c.gridy = 2;
            editorPanel.add(softTabsLabel,c);
            c.gridx = 1;
            c.gridy = 0;
            c.weightx = 1.0;
            c.anchor = c.LINE_END;
            editorPanel.add(tabWidthSpinner,c );
            c.gridy = 1;
            editorPanel.add(undoLimitSpinner, c );
            c.gridy = 2;
            editorPanel.add(softTabsCheckBox, c);
            // Output
            //c.weightx = 0.0;
            c.gridx = 0;
            c.gridy = 0;
            c.gridwidth = 2;
            c.anchor = c.LINE_START;
            c.fill = c.BOTH;
            c.weightx = 1.0;
            outputPanel.add( outputFontComboBox , c );
            c.gridwidth = 1;
            c.fill = c.NONE;
            c.weightx = 0.0;
            c.gridx = 2;
            c.anchor = c.LINE_END;
            outputPanel.add( outputFontSizeSpinner , c );
            c.gridx = 0;
            c.gridy = 1;
            c.anchor = c.LINE_START;
        
            
            
        // Add Panels to this
        c.fill = c.HORIZONTAL;
        c.anchor = c.NORTH;
        c.gridx = 0;
        c.gridy = 0;
        c.weightx = 1.0;
        c.weighty = 0.0;
        this.add(editorPanel,c);
        c.gridy = 1;
        this.add(outputPanel,c);

                
    }
    
    
    
    @Override
    public void save() {
        
        Log.log("Saving config");
        
        CollIDETextArea.getPropertyManager().setProperty("buffer.tabSize",
                ((Integer)this.tabWidthSpinner.getModel().getValue()).toString()
                );

        CollIDETextArea.getPropertyManager().setProperty("buffer.indentSize",
                ((Integer)this.tabWidthSpinner.getModel().getValue()).toString()
                );

        CollIDETextArea.getPropertyManager().setProperty("buffer.undoCount",
                ((Integer)this.undoLimitSpinner.getModel().getValue()).toString()
                );

        CollIDETextArea.getPropertyManager().setBooleanProperty("buffer.noTabs", softTabsCheckBox.isSelected());
        
        Config.setProperty("output.font",(String)outputFontComboBox.getSelectedItem());
        Config.setIntProperty("output.font.size", (Integer)outputFontSizeSpinner.getValue());
        
    }
    
    
    public void load() {
        
        Log.log("Loading config");
        
        // Get the Font Family list
        GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
        String[] fontFamilies = env.getAvailableFontFamilyNames();
        
        // Put the FontFamilies in the ComboBox
        int selected = 0;
        for (int i=0;i<fontFamilies.length;i++) {
            if ( fontFamilies[i].replace(" ","").equalsIgnoreCase( Config.getFontProperty("output.font").getFamily().replace(" ","") ) ) {
                selected = i;
            }
            outputFontComboBox.addItem( fontFamilies[i] );
        }
        
        // Select right Item
        outputFontComboBox.setSelectedIndex(selected);
        
    }
    
}
